const Controller = require('egg').Controller;

class AlumniController extends Controller {
    // 创建校友
    async create() {
        const { ctx, service } = this;

        try {
            // 参数验证
            const rules = {
                student_id: { type: 'string', required: true },
                real_name: { type: 'string', required: true },
                grade: { type: 'string', required: true },
                major: { type: 'string', required: true },
                college: { type: 'string', required: true },
                phone: { type: 'string', required: true },
                status: { type: 'int', required: false }
            };
            ctx.validate(rules);

            const alumniData = ctx.request.body;
            const result = await service.alumni.create(alumniData);

            ctx.body = {
                code: 0,
                data: result,
                msg: '创建成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '创建校友失败'
            };
        }
    }

    // 获取校友列表
    async list() {
        const { ctx, service } = this;

        try {
            const query = ctx.query;
            const result = await service.alumni.getAlumnis(query);

            ctx.body = {
                code: 0,
                data: result,
                msg: '获取成功'
            };
        } catch (error) {
            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '获取校友列表失败'
            };
        }
    }

    // 获取校友详情
    async detail() {
        const { ctx, service } = this;

        try {
            const { id } = ctx.params;

            // 验证ID参数
            const rules = {
                id: { type: 'id', required: true }
            };
            ctx.validate(rules, ctx.params);

            const alumni = await service.alumni.getAlumniById(id);

            ctx.body = {
                code: 0,
                data: alumni,
                msg: '获取成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '获取校友详情失败'
            };
        }
    }

    // 根据学号获取校友详情
    async detailByStudentId() {
        const { ctx, service } = this;

        try {
            const { student_id } = ctx.params;

            // 验证参数
            const rules = {
                student_id: { type: 'string', required: true }
            };
            ctx.validate(rules, ctx.params);

            const alumni = await service.alumni.getAlumniByStudentId(student_id);

            ctx.body = {
                code: 0,
                data: alumni,
                msg: '获取成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '获取校友详情失败'
            };
        }
    }

    // 更新校友信息
    async update() {
        const { ctx, service } = this;

        try {
            const { id } = ctx.params;
            const updateData = ctx.request.body;

            // 验证路径参数
            const paramRules = {
                id: { type: 'id', required: true }
            };
            ctx.validate(paramRules, ctx.params);

            // 验证body参数
            const bodyRules = {
                student_id: { type: 'string', required: false },
                real_name: { type: 'string', required: false },
                grade: { type: 'string', required: false },
                major: { type: 'string', required: false },
                college: { type: 'string', required: false },
                phone: { type: 'string', required: false },
                status: { type: 'int', required: false }
            };
            ctx.validate(bodyRules);

            const result = await service.alumni.updateAlumni(id, updateData);

            ctx.body = {
                code: 0,
                data: result,
                msg: '更新成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '更新校友信息失败'
            };
        }
    }

    // 删除校友
    async delete() {
        const { ctx, service } = this;

        try {
            const { id } = ctx.params;

            // 验证ID参数
            const rules = {
                id: { type: 'id', required: true }
            };
            ctx.validate(rules, ctx.params);

            await service.alumni.deleteAlumni(id);

            ctx.body = {
                code: 0,
                data: null,
                msg: '删除成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '删除校友失败'
            };
        }
    }

    // 批量导入校友
    async batchCreate() {
        const { ctx, service } = this;

        try {
            const { alumni_list } = ctx.request.body;

            // 验证参数
            const rules = {
                alumni_list: { type: 'array', required: true }
            };
            ctx.validate(rules);

            if (!Array.isArray(alumni_list) || alumni_list.length === 0) {
                throw new Error('校友列表不能为空');
            }

            // 验证每个校友数据的必填字段
            for (const alumni of alumni_list) {
                if (!alumni.student_id || !alumni.real_name || !alumni.grade || !alumni.major || !alumni.college) {
                    throw new Error('校友数据缺少必填字段');
                }
            }

            const result = await service.alumni.batchCreate(alumni_list);

            ctx.body = {
                code: 0,
                data: result,
                msg: `成功导入 ${result.length} 条校友数据`
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '批量导入校友失败'
            };
        }
    }

    // 获取统计信息
    async statistics() {
        const { ctx, service } = this;

        try {
            const result = await service.alumni.getStatistics();

            ctx.body = {
                code: 0,
                data: result,
                msg: '获取成功'
            };
        } catch (error) {
            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '获取统计信息失败'
            };
        }
    }

    // 校友认证
    async verify() {
        const { ctx, service } = this;

        try {
            // 参数验证
            const rules = {
                student_id: { type: 'string', required: true },
                real_name: { type: 'string', required: true },
                phone: { type: 'string', required: true }
            };
            ctx.validate(rules);

            const { student_id, real_name, phone } = ctx.request.body;

            // 验证校友信息
            const alumni = await service.alumni.verifyAlumni(student_id, real_name, phone);

            ctx.body = {
                code: 0,
                data: alumni,
                msg: '校友认证成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '校友认证失败'
            };
        }
    }

    // 获取认证状态
    async getVerificationStatus() {
        const { ctx, service } = this;

        try {
            const { student_id } = ctx.params;

            // 验证参数
            const rules = {
                student_id: { type: 'string', required: true }
            };
            ctx.validate(rules, ctx.params);

            const status = await service.alumni.getVerificationStatus(student_id);

            ctx.body = {
                code: 0,
                data: status,
                msg: '获取成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '获取认证状态失败'
            };
        }
    }
}

module.exports = AlumniController;